<?php
    require __DIR__ . '/vendor/autoload.php';
    use Intervention\Image\ImageManager;

    // 删除透明部分
    function cropTransparent($upload, $watermark_img)
    {
        $trim = imagecreatefrompng($upload);
        $cropped = imagecropauto($trim, IMG_CROP_DEFAULT);

        if ($cropped == false) {
            return false;
        }

        imagepng($cropped, $watermark_img);
        imagedestroy($cropped);
    }

    $now = time();
    $base = __DIR__ . '/data/base.png';
    $upload = __DIR__ . '/data/upload/'.$now.'.png';
    $watermark_img = __DIR__ . '/data/upload/trim_'.$now.'.png';
    $file = __DIR__ . '/data/images/'.$now.'.png';

    $img = Image::make($_FILES['image']['tmp_name']);
    $img->save($upload);

    if (cropTransparent($upload, $watermark_img) !== false) {
        $manager = new ImageManager();
        $img = $manager->make($base);
        $watermark = $manager->make($watermark_img)->resize(null, 55, function($constraint) {
            $constraint->aspectRatio();
        })->save();
        $img->insert($watermark_img, 'top-left', 850, 910);
        $img->save($file);
    } else {
        echo 'failed.';
    }
?>